package com.scfinance.SPD.encrypt.util;

import java.math.BigInteger;

import com.scfinance.SPD.encrypt.util.ConvertUtil;
import com.scfinance.SPD.encrypt.util.Constant;

public class RSAUtil {
	
//	private static final int RAS_ENCRYPT_LEN = 1024;
//	/*公钥加密*/
//	public static final String encryptKey(String encryptString) {
//		String data = ConvertUtil.bytesToHexString(encryptString.getBytes());
//		data = ConvertUtil.addSymbolAtRight(data, 256, '0');
//		BigInteger localBigInteger1 = null;
//		BigInteger localBigInteger2 = new BigInteger(data, 16);
//		if (encryptString.length() <= RAS_ENCRYPT_LEN) {
//			
//			localBigInteger1 = localBigInteger2.modPow(new BigInteger(getPublickey()), new BigInteger(getModule(), 16));
//		}
//		String hex = localBigInteger1.toString(16).toUpperCase();
//		return ConvertUtil.addSymbolAtLeft(hex, 256, '0');
//	}
//	
//	/*公钥解密*/
//	public static final String decryptKey(String decryptString) {
//		BigInteger biginteger = (new BigInteger(decryptString, 16)).modPow(new BigInteger(getPublickey()), new BigInteger(getModule(), 16));
//		String hexString = biginteger.toString(16);
//		return ConvertUtil.hexStringToByteStr(hexString.toUpperCase()).substring(0, 89);
//	}
	
	private static final int RAS_ENCRYPT_LEN = 1408;

	/* 公钥加密 */
	public static final String encryptKey(String encryptString) {
		String data = ConvertUtil.bytesToHexString(encryptString.getBytes());
		while (data.length() < 352) {
			data = "0" + data;
		}
		data = ConvertUtil.addSymbolAtRight(data, 352, '0');
		BigInteger localBigInteger1 = null;
		BigInteger localBigInteger2 = new BigInteger(data, 16);
		if (encryptString.length() <= RAS_ENCRYPT_LEN) {

			localBigInteger1 = localBigInteger2.modPow(new BigInteger(
					getPublickey()), new BigInteger(getModule(), 16));
		}
		String hex = localBigInteger1.toString(16).toUpperCase();
		return ConvertUtil.addSymbolAtLeft(hex, 352, '0');
	}

	/*公钥解密*/
	public static final String decryptKey(String decryptString) {
		BigInteger biginteger = (new BigInteger(decryptString, 16)).modPow(new BigInteger(getPublickey()), new BigInteger(getModule(), 16));
		String hexString = biginteger.toString(16);
		int index = hexString.indexOf("00");
		hexString = hexString.substring(index + 2);
		return ConvertUtil.hexStringToByteStr(hexString.toUpperCase());
	}
	
	/**
	 * 获取对应加密方式的加密密�?
	 * @return
	 */
	public static final String getPublickey(){
		String publickey = "";
		switch (ConstantUtil.ENCRYPTSTATUS) {
		case Constant.RSA:
			publickey=ConstantUtil.rsaPublickey;
			break;
		case Constant.UAT:
			publickey=ConstantUtil.uatPublickey;
			break;
		case Constant.PAT:
			publickey=ConstantUtil.patPublickey;
			break;
		case Constant.ZH:
			publickey=ConstantUtil.patPublickey;
			break;
		}
		return publickey;
	} 
	
	/**
	 * 获取对应加密方式的加密module
	 * @return
	 */
	public static final String getModule(){
		String module = "";
		switch (ConstantUtil.ENCRYPTSTATUS) {
		case Constant.RSA:
			module=ConstantUtil.rsaModule;
			break;
		case Constant.UAT:
			module=ConstantUtil.uatModule;
			break;
		case Constant.PAT:
			module=ConstantUtil.patModule;
			break;
		case Constant.ZH:
			module=ConstantUtil.patModule;
			break;
		}
		return module;
	} 
}
